package com.cn.xiaonuo.tcrfinance.tcrnews.service.Impl;

import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.vo.CommonParam;
import com.cn.xiaonuo.tcrfinance.tcrnews.entity.PvNoticeInfo;
import com.cn.xiaonuo.tcrfinance.tcrnews.mapper.HpcNewsMapper;
import com.cn.xiaonuo.tcrfinance.tcrnews.service.HpcNewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class HpcNewsServiceImpl implements HpcNewsService {

    @Autowired
    HpcNewsMapper hpcNewsMapper;

    /**
     * 公告信息
     *
     * @param commonParam
     */
    @Override
    public BusinessMessage selectNoticeInfos(CommonParam commonParam) {
        BusinessMessage message = new BusinessMessage();
        Integer pageSize = commonParam.getPageSize();
        Integer pageNo = commonParam.getPageNo();
        if (null == pageNo) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<PvNoticeInfo> pvNoticeInfos = hpcNewsMapper.selectNoticeInfos();
        PageInfo<PvNoticeInfo> pageInfo = new PageInfo<>(pvNoticeInfos);
        if (pvNoticeInfos.size() > 0) {
            message.setSuccess(true);
            message.setData(pageInfo);
            message.setMessage("操作成功");
            return message;
        } else {
            message.setCode("-1");
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    /**
     * 公告信息修改
     *
     * @param pvNoticeInfo
     */
    @Override
    public BusinessMessage updateNoticeInfo(PvNoticeInfo pvNoticeInfo) {
        BusinessMessage message = new BusinessMessage();
        Integer id = pvNoticeInfo.getId();
        String title = pvNoticeInfo.getNoticeTitle();
        String content = pvNoticeInfo.getNoticeContent();
        PvNoticeInfo pvNewsInfo = hpcNewsMapper.selectById(id);
        if (null == pvNewsInfo) {
            message.setSuccess(false);
            message.setCode("-1");
            message.setMessage("操作失败");
            return message;
        }
        pvNewsInfo.setNoticeTitle(title);
        pvNewsInfo.setNoticeContent(content);
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        pvNewsInfo.setCreateTime(currentTime);
        hpcNewsMapper.updateById(pvNoticeInfo);
        message.setSuccess(true);
        message.setCode("200");
        message.setMessage("操作成功");
        return message;
    }

    /**
     * 删除公告
     *
     * @param pvNoticeInfo
     */
    @Override
    public BusinessMessage deleteNoticeInfo(PvNoticeInfo pvNoticeInfo) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Integer id = pvNoticeInfo.getId();
        PvNoticeInfo pvNewsInfo = hpcNewsMapper.selectById(id);
        if (null == pvNewsInfo) {
            message.setSuccess(false);
            message.setCode("-1");
            message.setMessage("操作失败");
            return message;
        }
        hpcNewsMapper.deleteById(id);
        message.setSuccess(true);
        message.setCode("200");
        message.setMessage("操作成功");
        return message;
    }

    /**
     * 发布公告
     *
     * @param pvNoticeInfo
     */
    @Override
    public BusinessMessage insertNoticeInfo(PvNoticeInfo pvNoticeInfo) {
        BusinessMessage message = new BusinessMessage();
        String title = pvNoticeInfo.getNoticeTitle();
        String content = pvNoticeInfo.getNoticeContent();
        if (StringUtils.isBlank(title) || StringUtils.isBlank(content)) {
            message.setSuccess(false);
            message.setCode("-1");
            message.setMessage("操作失败");
            return message;
        }
        PvNoticeInfo noticeInfo = new PvNoticeInfo();
        noticeInfo.setNoticeTitle(title);
        noticeInfo.setNoticeContent(content);
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        noticeInfo.setCreateTime(currentTime);
        hpcNewsMapper.insert(noticeInfo);
        message.setSuccess(true);
        message.setCode("200");
        message.setMessage("操作成功");
        return message;
    }




}
